#定义元素和操作元素的方法
from selenium.webdriver.common.by import By
import time
from Base.basePage import BasePage

class LoginPage(BasePage):

    #元素
    username=(By.NAME,"accounts")  #用户名
    password=(By.NAME,"pwd")    #密码
    url="https://d2.shopxo.vip/login.html"   #url
    vertify=(By.NAME,"verify")     #验证码
    loginBtn=(By.CSS_SELECTOR,"body > div.body-content-container > div.body-content-formal-container > "
                              "div.am-g.user-login-container.am-padding-top-0.am-padding-bottom-0.theme-data-edit-event >"
                              " div > div.am-container > div.am-u-sm-12.am-u-md-6.am-u-lg-4.user-right-container > "
                              "div.am-radius-lg.am-background-white > div > "
                              "div.am-tabs-bd.am-border-0 > div.am-tab-panel.am-active > form > "
                              "div.am-form-group.am-form-group-refreshing.am-margin-top-lg.am-padding-0 > button")
    error_message=""


    #操作元素的方法
    def __init__(self, driver):
        super().__init__(driver)
        self.driver = driver
        self.url = "https://d2.shopxo.vip/login.html"

    def open(self):
        """打开登录页面"""
        self.driver.get(self.url)
        self.wait_for_element(self.loginBtn)
        return self

    def is_on_login_page(self):
        """检查是否在登录页面"""
        return "用户登录" in self.driver.title

    def input_username(self, username):
        """输入用户名/手机号/邮箱"""
        self.input_text(self.username, username)
        return self

    def input_password(self, password):
        """输入密码"""
        self.input_text(self.password, password)
        return self

    def input_vertify(self, vertify):
        """输入验证码"""
        self.input_text(self.vertify, vertify)
        return self


    def get_alertT_text(self):
        alert=self.driver.alert()
        self.error_message=alert.text()
        return self

    # def bypass_captcha(self):
    #     """绕过验证码处理 - 采用3种策略"""
    #     try:
    #         # 策略1: 尝试万能验证码（需要开发支持）
    #         self.input_captcha("000000")
    #
    #         # 策略2: 在测试环境下禁用验证码（需要环境配置）
    #         # 脚本层面无法直接实现
    #
    #         # 策略3: 通过开发者工具临时覆盖验证码（针对自动化测试环境）
    #         script = """
    #             document.querySelector("input[placeholder='验证码']").value = '000000';
    #             document.querySelector("input[name='verify']").value = '000000';
    #         """
    #         self.driver.execute_script(script)
    #
    #     except Exception as e:
    #         print(f"绕过验证码失败: {str(e)}")
    #     return self
    def bypass_vertify(self,val="0000"):
        js_script="window.verifyCaptcha=function(){return true}"
        self.driver.execute_script(js_script,val)
    def click_login_button(self):
        """点击登录按钮"""
        self.click_element(self.loginBtn)
        return self

    def login(self, username, password):
        """完整的登录流程"""
        self.open()
        self.input_username(username)
        self.input_password(password)
        #self.input_vertify(vertify)
        #self.bypass_vertify("0000")
        self.click_login_button()
        return self

    def get_error_message(self):
        """获取错误提示信息"""
        if self.is_element_present(self.error_message):
            return self.get_text(self.error_message)
        return None

    def is_login_successful(self):
        """检查是否登录成功"""
        # 检查是否跳转到用户中心页面
        if "联系我们-果博东方有限公司开户电话18108888862（客服）" in self.driver.title:
            return True
        else:
            return False
